<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\samply\WxBizDataCrypt;
use think\Session;
use think\Cache;
use think\Auth;
class Login extends Common
{	
	/*
     * 登录
     * @函数 index
     * @功能 添加申请预约信息
     * @参数 [mobile:账号,password:密码]
     */
    public function index()
    {	
    	 $userPhone =input('userPhone','');//用户名
         $loginPwd =input('loginPwd','');//密码
         $logStatus=(int)input('logStatus');//登录类型 1微信登录 2手机号密码登录
         $openId=input('openId','');//微信的openId
         if(empty($logStatus)){
            return json(['state'=>'203','msg'=>"登录类型为空"]);
         }else{
            if($logStatus==1){
                if(empty($openId)){
                    return json(['state'=>'205','msg'=>"微信登录失败"]);
                }else{
              $rs=Db::name('user_info')->field('userPhone,uid,accid,nickname,token')->where(['openId'=>$openId])->find();
                  if(!empty($rs['userPhone'])){
                     return json(['state'=>'200','mesg'=>"登录成功",'result'=>$rs]);
                  }else{
                     return json(['state'=>'204','mesg'=>"请前去绑定手机号"]);
                  }
                   
                }
            }else{
                if(empty($userPhone) || empty($loginPwd)){
                    return json(['state'=>'206','msg'=>"账号,密码不能为空"]);
                }else{
                    $userPwd=md5($loginPwd);
                    $arr=['userPhone'=>$userPhone,'loginPwd'=>$userPwd];
                  $data = Db::name('user_info')->where('userPhone',$userPhone)->find();  
                  if($data){
                     $result = Db::name('user_info')->where($arr)->field('uid,accid,nickname,token')->find();
                     if($result){
                        return json(['state'=>'200','msg'=>"登录成功",'result'=>$result]);
                     }else{
                        return json(['state'=>'201','msg'=>"密码输入错误"]);
                     }
                  }else{
                    return json(['state'=>'202','msg'=>"账号尚未注册"]);
                  }
                }
            }
         }
        } 
        //获取微信的openid
        public function openSel(){
        $code = input('code');//小程序传来的code值  
        if(empty($code)){
           return json(['state'=>'201','msg'=>'参数为空']);
        }
   $url ='https://api.weixin.qq.com/sns/jscode2session?appid=wx03c204fa32417649&secret=73ef388c4e2515e8d1e819a58b906d31&js_code=' . $code . '&grant_type=authorization_code';  
    //yourAppid为开发者appid.appSecret为开发者的appsecret,都可以从微信公众平台获取；  
    $info = file_get_contents($url);//发送HTTPs请求并获取返回的数据，推荐使用curl  
   
    $json = json_decode($info,true);//对json数据解码  
    if(!empty($json['errcode'])){
        return json(['state'=>$json['errcode'],'msg'=>$json['errmsg']]);
    }else{
      return json(['state'=>'200','msg'=>'获取成功','result'=>$json]);
    }
      } 
      //获取微信绑定的手机号
      public function getPhone(){
            $appid='wx03c204fa32417649';
            $sessionKey=input('sessionKey');
            $encryptedData=input('encryptedData');
             $iv =input('iv');
             if(empty($sessionKey) || empty($encryptedData) || empty($iv)){
                return json(['state'=>'203','msg'=>'参数不足']);
             }

            $pc =  new WXBizDataCrypt($appid, $sessionKey);
            //print_r($output);die;
            $errCode = $pc->decryptData($encryptedData, $iv, $data );

            if ($errCode == 0) {
               return json(['state'=>'200','msg'=>'获取成功','result'=>$data]);
            } else {
                return json(['state'=>$errCode,'msg'=>'获取失败再试试']);
            }

      }
      
      
    }